package com.huaru.message.scheduling;
import com.alibaba.fastjson.JSONObject;
import com.huaru.message.Vo.CourseVO;
import com.huaru.message.Vo.UsageRateVO;
import com.huaru.message.utile.Config;
import com.huaru.message.utile.CourseUtils;
import com.huaru.message.utile.SendMessageUtil;
import com.huaru.message.utile.UsageRateUtils;
import com.sun.management.OperatingSystemMXBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: hth
 * Remark: 定时任务放这里哦~~~
 * Time: 2023/6/7 10:24
 */
@Configuration
public class ScheduledTasks {
    private  static Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
    /**
     * 定时获取机器CPU 和 内存使用率 并推送
     */
    @Scheduled(cron = "0/3 * * * * *")
    public void  SendUsageRate(){
        //获取cpu使用率
        UsageRateVO usageRate = UsageRateUtils.getUsageRate();
        //站间推送
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("packType","UsageRate");
        JSONObject data = new JSONObject();
        String hostAddress = null;
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        data.put("ip",hostAddress);
        data.put("currentTime",new Date());
        data.put("usageRate",usageRate);
        jsonObject.put("data",data);
        String receiveAddress = Config.getString("receiveAddress");
        SendMessageUtil.Send(jsonObject,receiveAddress);
    }

    /**
     * 定时获取当前席位的所有进程列表
     */
    @Scheduled(cron = "0/5 * * * * *")
    public void  SendCourseList(){
        //获取所有进程列表
        List<CourseVO> courseList = CourseUtils.getCourseList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("packType","CourseList");
        JSONObject jsonObject1 = new JSONObject();
        String hostAddress = null;
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        //组装进程列表推送
        jsonObject1.put("ip",hostAddress);
        jsonObject1.put("courseList",courseList);
        jsonObject.put("data",jsonObject1);
        String receiveAddress = Config.getString("receiveAddress");
        SendMessageUtil.Send(jsonObject,receiveAddress);
    }

    /**
     * 定时获取机器CPU 和 内存使用率 并推送
     */
    @Scheduled(cron = "0/2 * * * * *")
    public void  SendSeatState(){
        //站间推送
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("packType","SeatState");
        JSONObject data = new JSONObject();
        String hostAddress = null;
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        data.put("ip",hostAddress);
        data.put("currentTime",new Date());
        jsonObject.put("data",data);
        String receiveAddress = Config.getString("receiveAddress");
        SendMessageUtil.Send(jsonObject,receiveAddress);
    }

}
